<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" @delDepts="getDepartments" @addDepts="addDepts" />
        <!-- 子菜单树形结构 -->
        <!-- default-expand-all：默认展示所有节点 -->
        <!--  -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <!-- <tree-tools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" /> -->
          <tree-tools slot-scope="obj" :tree-node="obj.data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
        </el-tree>
      </el-card></div>
    <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import treeTools from '@/views/departments/components/tree-tools'
import { getDepartments } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import AddDept from '@/views/departments/components/add-dept.vue'
export default {
  name: 'DepartmentsIndex',
  components: {
    treeTools,
    AddDept
  },
  props: {},
  data () {
    return {
      loading: false, // 配置全局加载状态来显示数据加载状态
      departs: [], //
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      company: {}, // 头部数据结构
      showDialog: false, // 显示窗体
      node: null
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getDepartments()
  },
  mounted () {},
  methods: {
    async getDepartments () {
      this.loading = true
      const result = await getDepartments()
      console.log(result)
      this.departs = transListToTreeData(result.depts, '') // 这里定义一个空串  因为 它是根 所有的子节点的数据pid 都是 ""
      // 修改跟曾没有id的情况，手动添加一个空字符串
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      console.log(this.company)
      this.loading = false
    },
    addDepts (node) {
      this.showDialog = true
      this.node = node
    },
    editDepts (node) {
      this.showDialog = true
      this.node = node
      // 此处通过获取组件实例来调用子组件的方法
      // 父调用子的方法
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>
<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
